'use strict';

angular.module('app')
  .directive('appPositionInfo', [
    '$http',
    function($http) {
      return {
        restrict: "A",
        replace: true,
        scope: {
          // isActive: "=",
          isLogin: "=",
          pos: "="
        },
        link: function(scope) {
          scope.pos.select = !!scope.pos.select || false;
          scope.$watch('pos', function(newVal) {
            if (newVal) {
              scope.pos.select = scope.pos.select || false;
              scope.imagePath = scope.pos.select ? 'image/star-active.png' : 'image/star.png';
            }
          })
          scope.imagePath = scope.pos.select ? "image/star-active.png" : "image/star.png";
          scope.favorite = function() {
            $http.post('data/myFavorite.json', {
                id: scope.pos.id,
                select: !scope.pos.select
              })
              .then(function(success) {
                scope.pos.select = !scope.pos.select;
                scope.imagePath = scope.pos.select ? 'image/star-active.png' : 'image/star.png';
              }, function(error) {
                // body...
              })
          }
        },
        templateUrl: "view/template/positionInfo.html"

      }
    }
  ])
